package demo11;

import java.util.ArrayList;
import java.util.Collections;

public class Demo {
	public static void main(String[] args) {
		ArrayList<Integer> list=new ArrayList<>();
		Collections.addAll(list, 5,10,20,50,100,200,500,800,1000);
		
		MyThread t1=new MyThread(list);
		MyThread t2=new MyThread(list);
		t1.setName("抽奖箱1");	t2.setName("抽奖箱2");
		t1.start(); t2.start();
	}
	public static class MyThread extends Thread {
		ArrayList<Integer> list;
		ArrayList<Integer> tota = new ArrayList<>();
		public MyThread(ArrayList<Integer> list){
			this.list=list;
		}
		@Override
		public void run() {
			while(true){
				synchronized (MyThread.class) {
					if(list.size()==0){
						System.out.print(getName()+":");
						tota.stream().forEach(t->System.out.print(t+","));
						System.out.println("总计金额为："+tota.stream().reduce(0, Integer::sum));
						break;
					}else{
						//抽奖
						Collections.shuffle(list);
						Integer prize = list.remove(0);
						tota.add(prize);
					}
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}

	}
}
